package com.taikang.hmp.hbusiness.diabetes.measure.job;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.HDConstant;
import com.taikang.hmp.frame.common.tools.DateTimeUtils;
import com.taikang.hmp.frame.user.bean.AuthUserInfo;
import com.taikang.hmp.hbusiness.diabetes.measure.job.service.ISugarMonitorAutoJobService;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.bean.SugarMonitor;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.bean.SugarPlan;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.service.ISugarService;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.service.SugarUtil;
import com.taikang.hmp.hbusiness.diabetes.measure.warn.service.IWarnService;

/** 
 * 血糖监测自动任务
 * author:qrp
 * time:2015年8月21日 下午1:49:33
 */
public class SugarMonitorAutoJob {
	private static Logger logger = LoggerFactory.getLogger(SugarMonitorAutoJob.class);

	/**
	 * 是否执行两周一次方案
	 */
	//public static int is_execute = 0; 
	
	
	private ISugarMonitorAutoJobService sugarMonitorAutoJobService;
	private ISugarService sugarService;
	private IWarnService warnService;


	public ISugarMonitorAutoJobService getSugarMonitorAutoJobService() {
		return sugarMonitorAutoJobService;
	}

	public void setSugarMonitorAutoJobService(
			ISugarMonitorAutoJobService sugarMonitorAutoJobService) {
		this.sugarMonitorAutoJobService = sugarMonitorAutoJobService;
	}

	public IWarnService getWarnService() {
		return warnService;
	}

	public void setWarnService(IWarnService warnService) {
		this.warnService = warnService;
	}
	
	public ISugarService getSugarService() {
		return sugarService;
	}

	public void setSugarService(ISugarService sugarService) {
		this.sugarService = sugarService;
	}
	
	
	
	
	public void genSugarPlan(){
		logger.info("血糖监测计划批处理启动......");
		
		
		List<AuthUserInfo> authUserInfo =sugarMonitorAutoJobService.getAllUserInfo();
		String currDate = DateTimeUtils.getCurrentDate();
		for (int i = 0; i < authUserInfo.size(); i++) {
			Boolean is_setWarn = false;
			String customerid = authUserInfo.get(i).getCustom_id();
//			if(!customerid.equals("DI107")){
//				continue;
//			}
			// 取得自己的血糖计划编码
			String sugarPlanCode = sugarMonitorAutoJobService.designSugarPlanCode(customerid);
			if(StringUtils.isEmpty(sugarPlanCode)){
				logger.info("customerid["+customerid+"]血糖计划编码设计为空！");
			}else{
				logger.info("customerid["+customerid+"]血糖计划编码推导结论："+sugarPlanCode);
			}
			
			// 根据血糖计划编码获取自己的血糖测量计划
			List<SugarMonitor> toSugarPlans = SugarUtil.convertToSugarPlans(sugarPlanCode);
						
			// 循环保存血糖测量计划
			for (int j = 0; j < toSugarPlans.size(); j++) {
				if(toSugarPlans.get(j).getTime() == 7){
					logger.info("customerid["+customerid+"]血糖测量计划：每周7天......");
					//本周周一
					String currendDay = DateTimeUtils.getCurrentDate();
					Date monday = DateTimeUtils.parseDate(DateTimeUtils.getRelativeMonday(currendDay));
					for (int k = 0; k < 7; k++) {
						int temp = k+1 - SugarUtil.getWeekOfDate(new Date());
						//String weekday = DateTimeUtils.formatDate(DateTimeUtils.getAfterDay(monday, k));
						SugarPlan sugarPlan = new SugarPlan();
						String plan_id = java.util.UUID.randomUUID().toString();
						sugarPlan = toSugarPlans.get(j).getSugarPlan();
						sugarPlan.setPlan_id(plan_id);
						sugarPlan.setDate(DateTimeUtils.formatDate(SugarUtil.getDateAfter(new Date(), temp)));
						sugarPlan.setCustomer_id(customerid);
						sugarPlan.setSource_cd(HDConstant.APP_SOURCE_DIABETES);
						sugarPlan.setOperater("batch");
						sugarService.insertOrUpdateSugarPlan(sugarPlan);
						is_setWarn = true;
					}
				}else{
					logger.info("customerid["+customerid+"]血糖测量计划：周"+toSugarPlans.get(j).getTime()+"......");
					SugarPlan sugarPlan = new SugarPlan();
					String plan_id = java.util.UUID.randomUUID().toString();
					int temp = toSugarPlans.get(j).getTime() - SugarUtil.getWeekOfDate(new Date());
					sugarPlan = toSugarPlans.get(j).getSugarPlan();
					sugarPlan.setPlan_id(plan_id);
					sugarPlan.setCustomer_id(customerid);
					sugarPlan.setDate(DateTimeUtils.formatDate(SugarUtil.getDateAfter(new Date(), temp)));
					sugarPlan.setSource_cd(HDConstant.APP_SOURCE_DIABETES);
					sugarPlan.setOperater("batch");
					sugarService.insertOrUpdateSugarPlan(sugarPlan);
					is_setWarn = true;
				//}else if(is_execute == 1){
				}
			}
			//判断是否胰岛素治疗,如果是给出胰岛素方案2
			if(sugarMonitorAutoJobService.is_check(customerid) && new Date().getDate()%2==0){
				logger.info("customerid["+customerid+"]胰岛素血糖测量计划......");
				SugarPlan sugarPlan = new SugarPlan();
				List<SugarMonitor> info1 = SugarUtil.convertToSugarPlans("0#2");
				sugarPlan = info1.get(0).getSugarPlan();
				String plan_id = java.util.UUID.randomUUID().toString();
				
				sugarPlan.setPlan_id(plan_id);
				sugarPlan.setCustomer_id(customerid);
				sugarPlan.setDate(DateTimeUtils.formatDate(SugarUtil.getDateAfter(new Date(), 6)));
				sugarPlan.setSource_cd(HDConstant.APP_SOURCE_DIABETES);
				sugarPlan.setOperater("batch");
				sugarService.insertOrUpdateSugarPlan(sugarPlan);
				is_setWarn = true;
			}
			//给该用户设置提醒消息
			if(is_setWarn){
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("customer_id" , customerid);
				jsonObj.put("isread"      , "0");
				jsonObj.put("warnmsg"     , "你有新的血糖测量计划已更新，请注意查看。");
				jsonObj.put("warntypecd"  , "通知");
				jsonObj.put("date"        , currDate);
				warnService.saveWarn(jsonObj);
			}
		}
			
		logger.info("血糖监测计划批处理结束.");
	}
}
